﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;


using Windows.Phone.Devices.Power;
using Windows.Phone.Speech.Synthesis;


namespace WinPhoneVoiceCommands
{
    public partial class Page1 : PhoneApplicationPage
    {
        private string result;
        private string queryString;

        public Page1()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            foreach (KeyValuePair<string, string> kvp in NavigationContext.QueryString)
            {
                this.queryString += "["+kvp.Key+"]=";
                this.queryString += "[" + kvp.Value + "]";
                this.queryString += "\n";
            }

            if (NavigationContext.QueryString.ContainsKey("voiceCommandName"))
            {
                string voicecommandname = NavigationContext.QueryString["voiceCommandName"];
                switch (voicecommandname)
                {
                    case "Util":
                        string options = NavigationContext.QueryString["options"];
                        DoOperation(options);
                        break;

                    case "Calculator":
                        string number1 = NavigationContext.QueryString["operand1"];
                        string number2 = NavigationContext.QueryString["operand2"];
                        string oprator = NavigationContext.QueryString["operation"];
                        try
                        {
                            DoCalculation(int.Parse(number1),int.Parse(number2),oprator);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message+"\n"+ex.StackTrace);
                        }
                        break;
                    
                    default:
                        break;
                }
            }

            if(this.result!=null)
                this.TxtResult.Text = "Result is: " + this.result;
            if (this.queryString != null)
                this.TxtQString.Text = this.queryString;
        }

        private int GetBatteryPercentage()
        {
            Battery btr = Battery.GetDefault();
            return  btr.RemainingChargePercent;
        }

       
        private void DoCalculation(int num1, int num2,string oprtr)
        {
            switch (oprtr)
            {
                case "más":
                case "plus":
                    this.result = (num1 + num2).ToString();
                    break;

                case "menos":
                case "minus":
                    this.result = (num1 - num2).ToString();
                    break;

                default:
                    break;
            }
        }

        private void DoOperation(string command)
        {
            switch(command)
            {
                case "fecha":
                case "date":
                    this.result = DateTime.Now.Month + "/" + DateTime.Now.Day + "/" + DateTime.Now.Year;
                    break;

                case "tiempo":
                case "time":
                    this.result = DateTime.Now.Hour + ":" + DateTime.Now.Minute;
                    break;

                case "batería porcentaje":
                case "battery percentage":
                    this.result = GetBatteryPercentage().ToString();
                    break;

                default:
                   break;
            }
        }
    }
}